package com.grandbroad.eim.ui.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.west.westclient.api.login.bean.User;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.IconMenuAdapter;
import com.grandbroad.eim.base.BaseFragment;
import com.grandbroad.eim.entry.ClassIconBean;
import com.grandbroad.eim.entry.WhcatBean;
import com.grandbroad.eim.ui.activities.CollectActivity;
import com.grandbroad.eim.ui.activities.FollowListActivity;
import com.grandbroad.eim.ui.activities.LoginActivity;
import com.grandbroad.eim.ui.activities.UserInfoActivity;
import com.grandbroad.eim.ui.activities.VipActivity;
import com.grandbroad.eim.utils.CacheUtils;
import com.orhanobut.hawk.Hawk;

import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MeFragment extends BaseFragment {
    @BindView(R.id.tv_userinfo)
    TextView tvUserinfo;
    @BindView(R.id.img_my_avatar)
    ImageView imgMyAvatar;
    @BindView(R.id.cd_my_avatar)
    CardView cdMyAvatar;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.recycler_my_menu)
    RecyclerView recyclerMyMenu;
    Unbinder unbinder;
    @BindView(R.id.li_vip_info)
    LinearLayout liVipInfo;
    @BindView(R.id.li_my_order)
    LinearLayout liMyOrder;
    @BindView(R.id.li_my_setting)
    LinearLayout liMySetting;
    @BindView(R.id.li_my_about)
    LinearLayout liMyAbout;
    @BindView(R.id.li_my_logout)
    LinearLayout liMyLogout;
    Unbinder unbinder1;
    private IconMenuAdapter menuAdapter;
    private User user;
    private WhcatBean whcatBean;
    @Override
    protected boolean setEventOpen() {
        return false;
    }

    @Override
    protected void initdata() {
        user = CacheUtils.getUser();
    }

    @Override
    protected void initview() {
        menuAdapter = new IconMenuAdapter(getActivity());
        recyclerMyMenu.setLayoutManager(new GridLayoutManager(getContext(),3,GridLayoutManager.VERTICAL,false));
        recyclerMyMenu.setAdapter(menuAdapter);
        whcatBean = Hawk.get("wechat");

        if (whcatBean!=null&&TextUtils.isEmpty(user.getAvatar()))
        {
            //如果用户直接通过微信登陆直接采用微信的数据，头像和名字
            x.image().bind(imgMyAvatar,whcatBean.getHeadimgurl());
            tvUserName.setText(whcatBean.getNickname());
        }
        else
        {
            //加载头像
            x.image().bind(imgMyAvatar,user.getAvatar());
            //设置昵称
            tvUserName.setText(user.getName());
        }

        inisession();
    }

    private void inisession() {

        //个人中心点击事件
        tvUserinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserInfoActivity.class,false);
            }
        });
        //vip点击事件
        liVipInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(VipActivity.class,false);
            }
        });
        //退出登陆
        liMyLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog(true, "提示", "是否退出登陆?", null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CacheUtils.loginout();
                        startActivity(LoginActivity.class,true);
                    }
                });
            }
        });
        //设置图标点击事件
        menuAdapter.setIconClickListener(new IconMenuAdapter.OnIconClickListener() {
            @Override
            public void click(int pos, ClassIconBean iconBean) {
                switch (iconBean.getId())
                {
                    case 1:
                        //前往关注界面
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bean",CacheUtils.getUser());
                        startActivityWithBundle(FollowListActivity.class,false,bundle);
                        break;
                    case 2:
                        //前往收藏界面
                        Bundle bundle1 = new Bundle();
                        bundle1.putSerializable("bean",CacheUtils.getUser());
                        startActivityWithBundle(CollectActivity.class,false,bundle1);
                        break;

                }
            }
        });

    }


    @Override
    protected int setlayout()
    {
        return R.layout.fragment_me;
    }


}
